<?php
	include("cronDB.config.php");
	
	$fileName = "logs/CAMPAIGN_EMAILS_".strtoupper(date('dMo_Hi')).".log";
	$fileHandle = fopen($fileName,"w");

	writeLogMessage("File " . $fileName .  " Open and Ready for Writing", $fileHandle);
		
	$sql = "select * from campaign_emails where campaign_id='P672_DEVO'";
	
	$DB->query($sql);
	
	writeLogMessage("Query Executed - " . $DB->get_num_rows() . " Rows Returned", $fileHandle);
	
	while($row = $DB->fetch_row()) {
		if(emailUser($row['campaign_email'])) {
			writeLogMessage("Success - Email Sent to " . $row['campaign_email'], $fileHandle);
		}
		else {
			writeLogMessage("Failure - Email Sent to " . $row['campaign_email'], $fileHandle);
		}
	}
	
	writeLogMessage("All Emails Sent", $fileHandle);
	
	fclose($fileHandle);
	
	function emailUser($emailAddress) {
		
		$emailText .= "Your email has been added to a list to recieve daily devotionals for Project 672.\n\n";
		$emailText .= " - Today's devotional can be found at http://www.lwbc.net/LivingWeb/Upload/ENDEAVOR/Project672/" . date("mdy") . ".pdf\n";
		$emailText .= " - Past devotionals can be downloaded at http://www.lwbc.net/index.php?PID=533\n\n";
		$emailText .= "Thank you for partnering with us in this ministry.\n\n";
		$emailText .= "If you are recieving this email in error or if any informaiton in it is wrong, then feel free to email project672@lwbc.net.\n\n";
		$emailText .= "Living Word Online\n(434) 525-7736\n";
			
		if(mail($emailAddress ,"Project 672 Daily Devotional",$emailText,"From: Living Word Baptist Church <admin@lwbc.net>" . "\r\n" . "Reply-To: admin@lwbc.net")) {
			return true;
		}
		else {
			return false;
		}
	}
	
	function writeLogMessage($message, $fileHandle) {
		$message = date("d-M-o H:i:s") . "   -   " . $message . "\r\n";
		print $message;
		fwrite($fileHandle, $message);
	}
?>